package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.MemberRecords;
import org.dromara.system.domain.bo.MemberRecordsBo;
import org.dromara.system.domain.bo.OpenMemberShipBo;
import org.dromara.system.domain.vo.MemberRecordsVo;
import org.dromara.system.service.IMemberRecordsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * 会员开通记录
 *
 * @author Lion Li
 * @date 2025-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/memberRecords")
public class MemberRecordsController extends BaseController {

    private final IMemberRecordsService memberRecordsService;

    /**
     * 查询会员开通记录列表
     */
//    @SaCheckPermission("system:records:list")
    @GetMapping("/list")
    public TableDataInfo<MemberRecordsVo> list(MemberRecordsBo bo, PageQuery pageQuery) {
        return memberRecordsService.queryPageList(bo, pageQuery);
    }


    //    @SaCheckPermission("system:records:list")
    @GetMapping("/listReception")
    public TableDataInfo<MemberRecordsVo> listReception(MemberRecordsBo bo, PageQuery pageQuery) {
        return memberRecordsService.listReception(bo, pageQuery);
    }

    /**
     * 导出会员开通记录列表
     */
//    @SaCheckPermission("system:records:export")
    @Log(title = "会员开通记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MemberRecordsBo bo, HttpServletResponse response) {
        List<MemberRecordsVo> list = memberRecordsService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员开通记录", MemberRecordsVo.class, response);
    }

    /**
     * 获取会员开通记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:records:query")
    @GetMapping("/{id}")
    public R<MemberRecordsVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(memberRecordsService.queryById(id));
    }

    /**
     * 新增会员开通记录
     */
//    @SaCheckPermission("system:records:add")
    @Log(title = "会员开通记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MemberRecordsBo bo) {
        return toAjax(memberRecordsService.insertByBo(bo));
    }

    /**
     * 修改会员开通记录
     */
//    @SaCheckPermission("system:records:edit")
    @Log(title = "会员开通记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MemberRecordsBo bo) {
        return toAjax(memberRecordsService.updateByBo(bo));
    }

    /**
     * 删除会员开通记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:records:remove")
    @Log(title = "会员开通记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(memberRecordsService.deleteWithValidByIds(List.of(ids), true));
    }

    //    @SaCheckPermission("system:records:remove")
//    @Log(title = "开通会员", businessType = BusinessType.OTHER)
    @PostMapping("/openMemberShip")
    public R<String> openMemberShip(@RequestBody OpenMemberShipBo openMemberShipBo) {
        return R.ok("支付拉起成功！", memberRecordsService.openMemberShip(openMemberShipBo));
    }

    //    @Log(title = "开通会员支付宝回调", businessType = BusinessType.OTHER)
    @PostMapping("/openMemberShip/callBak/alipay")
    @SaIgnore
    public String aliPayCallback(HttpServletRequest request) {
        return memberRecordsService.aliPayCallback(request);

    }

    @PostMapping(value = "/openMemberShip/callBak/wxPay")
    @SaIgnore
    public String wxPayCallback(HttpServletRequest request, HttpServletResponse response) {
        return memberRecordsService.wxPayCallback(request, response);
    }

    @PostMapping("/queryMemberPrice")
    public R<BigDecimal> queryMemberPrice() {
        return R.ok("查询成功！", memberRecordsService.queryMemberPrice());
    }

    @PostMapping("/testMemberCall/{id}")
    public void testMemberCall(@PathVariable String id) {
        MemberRecords memberRecords = memberRecordsService.lambdaQuery().eq(MemberRecords::getId, id).one();
        memberRecordsService.dealMemberWork(memberRecords);
    }

}
